package com.example.calculatorautomation

import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        try {
            super.onCreate(savedInstanceState)
            Log.d("CalcDemo", "MainActivity onCreate - start") // 更早的日志
            setContentView(R.layout.activity_main)

            val inputEditText = findViewById<EditText>(R.id.inputEditText)
            val btnOpenAccessibility = findViewById<Button>(R.id.btnOpenAccessibility)
            val btnCalc = findViewById<Button>(R.id.btnCalc)

            btnOpenAccessibility.setOnClickListener {
                Log.d("CalcDemo", "点击了打开无障碍设置按钮")
                try {
                    val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
                    startActivity(intent)
                } catch (e: Exception) {
                    Log.e("CalcDemo", "打开无障碍设置失败: ${e.message}")
                }
            }

            btnCalc.setOnClickListener {
                Log.d("CalcDemo", "点击了自动计算按钮")
                val formula = inputEditText.text.toString()
                if (formula.isBlank()) {
                    Log.e("CalcDemo", "公式为空")
                    return@setOnClickListener
                }
                Log.d("CalcDemo", "发送公式到无障碍服务: $formula")
                MyAccessibilityService.enqueueFormula(formula)
            }
        } catch (e: Exception) {
            Log.e("CalcDemo", "MainActivity onCreate Exception: ${e.message}", e)
        }
    }
}
